package com.lmxdawn.api.dnf.job;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.lmxdawn.api.admin.entity.GoldCoin;
import com.lmxdawn.api.dnf.entity.Order;
import com.lmxdawn.api.dnf.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderJob {


    @Resource
    OrderService orderService;
    //推送方法，每秒执行一次
    @Scheduled(fixedRate = 60*1000)
    public void check() throws InterruptedException {
        log.info("订单收货确认任务开始");
        List<Order> orderList = orderService.lambdaQuery()
                .eq(Order::getPaymentStatus, 1)
                .notIn(Order::getType, Arrays.asList(3, 15))
                .eq(Order::getStatus, 5).list();
        for (Order order : orderList) {
            if (order.getUpdateTime() != null){
                // 如果时间超过了有效期 进行下架
                if (DateUtil.between(new Date(), order.getUpdateTime() , DateUnit.MINUTE) > 60){
                    order.setStatus(1);
                    log.info("订单：{}，自动确认收货", order.getId());
                    orderService.confirmReceive(order.getId().toString(), order.getUserId(), 2);
                    log.info("订单：{}，自动确认收货完成", order.getId());
                }
            }
        }
    }
 
}